//
//  ModifyDealPassword.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/30.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "ModifyDealPassword.h"
#import "UserInfoTableVC.h"
#import "ForgetPassword.h"
@interface ModifyDealPassword () {
    NSInteger pointRadius;
}

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) NSArray *imageArray;

@property (weak, nonatomic) IBOutlet UIImageView *firstLineR;
@property (weak, nonatomic) IBOutlet UIImageView *firstImages;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;

@property (weak, nonatomic) IBOutlet UIImageView *secondLineR;
@property (weak, nonatomic) IBOutlet UIImageView *secondLineL;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;
@property (weak, nonatomic) IBOutlet UIImageView *secondImage;

@property (weak, nonatomic) IBOutlet UILabel *thirdLabel;
@property (weak, nonatomic) IBOutlet UIImageView *thirdImage;
@property (weak, nonatomic) IBOutlet UIImageView *thirdLineL;


@property (weak, nonatomic) IBOutlet UIView *passwordView;

@end

@implementation ModifyDealPassword

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"修改交易密码"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    _num = 0;
    [self passwordEdit];
    [self hideAllImage];
    [self textAlert];
    _dealPwd = [NSMutableString string];
    [_textField becomeFirstResponder];
    [_textField addTarget:self action:@selector(textValueChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)passwordEdit {
    pointRadius = 10;
    _img1.layer.cornerRadius = pointRadius;
    _img1.layer.masksToBounds = YES;
    _img2.layer.cornerRadius = pointRadius;
    _img2.layer.masksToBounds = YES;
    _img3.layer.cornerRadius = pointRadius;
    _img3.layer.masksToBounds = YES;
    _img4.layer.cornerRadius = pointRadius;
    _img4.layer.masksToBounds = YES;
    _img5.layer.cornerRadius = pointRadius;
    _img5.layer.masksToBounds = YES;
    _img6.layer.cornerRadius = pointRadius;
    _img6.layer.masksToBounds = YES;
    _imageArray = @[_img1,_img2,_img3,_img4,_img5,_img6];
    
    _passwordView.layer.cornerRadius = 5;
    _passwordView.layer.borderColor = RGB(164, 164, 164).CGColor;
    _passwordView.layer.borderWidth = 1;
    _passwordView.layer.masksToBounds = YES;
    for (UIView *view in _passwordView.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            view.layer.borderColor = RGB(164, 164, 164).CGColor;
            view.layer.borderWidth = 1;
            view.layer.masksToBounds = YES;
        }
    }
}

- (void)textValueChange:(UITextField *)textView {
    NSInteger length = textView.text.length;
    for (NSInteger i = 0; i < _imageArray.count; i++) {
        UIImageView *image = _imageArray[i];
        if (i < length) {
            image.hidden = NO;
        }else {
            image.hidden = YES;
        }
    }
    if (length == 6) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self changeDealPwd];
        });
    }
}

- (void)changeDealPwd {
    if (_num == 0) {
        _oldPwd = _textField.text;
    }else if (_num == 1) {
        _recentPwd = _textField.text;
    }else if (_num == 2) {
        _confirmPwd = _textField.text;
        [self changePassword];
        return;
    }
    _num ++;
    [self textAlert];
}
//editPS_进度条  editPS_矩形4 editPS_未进行 editPS_正在进行
- (void)textAlert {
    if (_num == 0) {
        self.titleLabel.text = @"输入原交易密码";
        _firstLabel.textColor = BLUE_COLOR;
        _firstLineR.image = IMAGE(@"editPS_进度条");
        _firstImages.image = IMAGE(@"editPS_正在进行");
        _secondLineL.image = IMAGE(@"editPS_进度条");
        _secondLineR.image = IMAGE(@"editPS_进度条");
        _secondLabel.textColor = DEFAULTCOLOR;
        _secondImage.image = IMAGE(@"editPS_未进行");
        _thirdImage.image = IMAGE(@"editPS_未进行");
        _thirdLineL.image = IMAGE(@"editPS_进度条");
        _thirdLabel.textColor = DEFAULTCOLOR;
        _forgetBtn.hidden = NO;
    } else if (_num == 1) {
        self.titleLabel.text = @"设置新的交易密码";
        _firstLabel.textColor = DEFAULTCOLOR;
        _firstLineR.image = IMAGE(@"editPS_矩形4");
        _firstImages.image = IMAGE(@"editPS_正在进行");
        _secondLineL.image = IMAGE(@"editPS_矩形4");
        _secondLineR.image = IMAGE(@"editPS_进度条");
        _secondLabel.textColor = BLUE_COLOR;
        _secondImage.image = IMAGE(@"editPS_正在进行");
        _thirdImage.image = IMAGE(@"editPS_未进行");
        _thirdLineL.image = IMAGE(@"editPS_进度条");
        _thirdLabel.textColor = DEFAULTCOLOR;
        _forgetBtn.hidden = YES;
    } else if(_num == 2) {
        self.titleLabel.text = @"确认新的交易密码";
        _firstLabel.textColor = DEFAULTCOLOR;
        _firstLineR.image = IMAGE(@"editPS_矩形4");
        _firstImages.image = IMAGE(@"editPS_正在进行");
        _secondLineL.image = IMAGE(@"editPS_矩形4");
        _secondLineR.image = IMAGE(@"editPS_矩形4");
        _secondLabel.textColor = DEFAULTCOLOR;
        _secondImage.image = IMAGE(@"editPS_正在进行");
        _thirdImage.image = IMAGE(@"editPS_正在进行");
        _thirdLineL.image = IMAGE(@"editPS_矩形4");
        _thirdLabel.textColor = BLUE_COLOR;
        _forgetBtn.hidden = YES;
    }
     [self hideAllImage];
}

- (void)changePassword {
    if (![_recentPwd isEqualToString:_confirmPwd]) {
        JSSHOW(@"两次输入密码不一致");
        _num = 1;
        [self textAlert];
        return;
    }
    JSSHOWHUD;
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"User/editTransactionPwd";
    request.params = @{@"pwd":_oldPwd,@"npwd":_recentPwd};
    [DZNetAPI request:request success:^(id response) {
        NSLog(@"%@",response);
        JSHIDEHUD;
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            [CommTools setUserInfo:response];
            JSSHOW(@"交易密码修改成功");
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            JSSHOW(model.msg);
            _num = 0;
            [self textAlert];
        }
    } failure:^(NSError *error) {
        JSHIDEHUD;
        JSSHOW(LOAD_FAIL_TIP);
    }];
    
}

- (void)hideAllImage {
    _img1.hidden = YES;
    _img2.hidden = YES;
    _img3.hidden = YES;
    _img4.hidden = YES;
    _img5.hidden = YES;
    _img6.hidden = YES;
    _textField.text = @"";
    _dealPwd = [NSMutableString string];
}

- (IBAction)forgetPassword:(id)sender {
    ForgetPassword *forget = [[ForgetPassword alloc] init];
    [forget setupTitleView:@"忘记交易密码"];
    forget.telStr = DZUserInfo.userInfo.mobile;
    forget.sms_type = [NSString stringWithFormat:@"%d",3];
    UINavigationController *nav = self.navigationController;
    [nav popViewControllerAnimated:NO];
    [nav pushViewController:forget animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
